package com.rxyl.love.petdoctor.acticity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.adapter.CommentListAdapter;
import com.rxyl.love.petdoctor.adapter.MyGridViewAdapter;
import com.rxyl.love.petdoctor.adapter.MyListViewAdapter;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.constant.Urls;
import com.rxyl.love.petdoctor.entity.HospitalComment;
import com.rxyl.love.petdoctor.entity.HospitalDetail;
import com.rxyl.love.petdoctor.entity.HospitalDoctor;
import com.rxyl.love.petdoctor.util.NetworkUtil;
import com.rxyl.love.petdoctor.view.MyListView;

import java.util.ArrayList;
import java.util.List;

public class HospitalActivity extends AppCompatActivity {
    private int hospitalId;

    private TextView name, price, score, hours, address, phone;
    private ImageView head;
    private RelativeLayout rlHospitalPhone, rlHospitalAddress;

    private String hospitalUrl, headUrl, doctorUrl, commentUrl;

    private GridView gridView;
    private MyGridViewAdapter gvAdapter;
    private HospitalDetail.DataEntity datas;
    private List<HospitalDoctor.DataEntity> doctorList = new ArrayList<>();

    private ListView lvComment;
    private CommentListAdapter lvAdapter;
    private List<HospitalComment.DataEntity.ListEntity> commentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital);
        init();
        loadHospital();
        loadDoctor();
        loadComment();
    }

    private void init() {
        Intent intent = getIntent();
        hospitalId = intent.getIntExtra("hospitalId", -1);

        name = (TextView) findViewById(R.id.tv_hospital_detail_name);
        price = (TextView) findViewById(R.id.tv_hospital_detail_price);
        score = (TextView) findViewById(R.id.tv_hospital_detail_score);
        head = (ImageView) findViewById(R.id.iv_hospital_detail_head);
        hours = (TextView) findViewById(R.id.tv_hospital_hours);
        address = (TextView) findViewById(R.id.tv_hospital_address);
        phone = (TextView) findViewById(R.id.tv_hospital_phone);
        gridView = (GridView) findViewById(R.id.gridView_hospital);
        lvComment = (ListView) findViewById(R.id.lv_hospital_comment);
        rlHospitalAddress  = (RelativeLayout) findViewById(R.id.rl_hospital_address);

        gvAdapter = new MyGridViewAdapter(this, doctorList);
        gridView.setAdapter(gvAdapter);

        lvAdapter = new CommentListAdapter(this, commentList);
        lvComment.setAdapter(lvAdapter);


    }

    private void loadHospital() {
        hospitalUrl = Urls.HOSPITAL_DETAIL + hospitalId;
        NetworkUtil.getInstance().getHospital(hospitalUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                HospitalDetail hospitalDetail = gson.fromJson(responseInfo.result.toString(),
                        HospitalDetail.class);
                if (hospitalDetail != null && hospitalDetail.getData() != null) {
                    datas = hospitalDetail.getData();
                    setData();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });

    }

    private void loadDoctor() {
        doctorUrl = Urls.HOSPITAL_DOCTOR + hospitalId;
        NetworkUtil.getInstance().getGoodDoctor(doctorUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                HospitalDoctor hospitalDoctor = gson.fromJson(responseInfo.result.toString(),
                        HospitalDoctor.class);
                if (hospitalDoctor != null && hospitalDoctor.getData() != null) {
                    doctorList.clear();
                    doctorList.addAll(hospitalDoctor.getData());
                    gvAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    //id=907&type=1&sort=1&limit=5&page=1
    private void loadComment() {
        commentUrl = Urls.COMMENT + hospitalId + "&type=1&sort=1&limit=5&page=1";
        NetworkUtil.getInstance().getComment(commentUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                HospitalComment hospitalComment = gson.fromJson(responseInfo.result.toString(),
                        HospitalComment.class);
                if (hospitalComment != null && hospitalComment.getData() != null) {
                    commentList.clear();
                    commentList.addAll(hospitalComment.getData().getList());
                    lvAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    private void setData() {
        name.setText(datas.getName());
        price.setText(datas.getAvg_price() + "");
        headUrl = datas.getPhoto_url();
        MyApplication.getApp().getImageLoader().displayImage(headUrl, head,
                MyApplication.getApp().getImageOptions());
        hours.setText(datas.getHours());
        address.setText(datas.getAddress());
        phone.setText(datas.getTelephone());

        rlHospitalPhone = (RelativeLayout) findViewById(R.id.rl_hospital_phone);
        rlHospitalPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel://" + phone.getText()));
                startActivity(intent);
            }
        });

        rlHospitalAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到地图页面
                Intent intent = new Intent(HospitalActivity.this, RoutePlanActivity.class);
                intent.putExtra("hospitalLatitude",datas.getLatitude());
                intent.putExtra("hospitalLongitude", datas.getLongitude());
                startActivity(intent);
            }
        });
    }

    public void backTo(View view) {
        this.finish();
    }

}
